package pl.nazaweb.imedproplus.business.service.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlLoader {

    private Document xmlDocument;

    public void loadDocument(String fileName)
            throws ParserConfigurationException, SAXException, IOException {
        File xmlFile = new File(fileName);
        if (xmlFile.exists()) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            this.xmlDocument = builder.parse(xmlFile);
        } else {
            throw new FileNotFoundException("Nie znaleziono pliku xml: ".concat(fileName));
        }
    }

    public NodeList getDocumentNodeList() {
        return this.xmlDocument.getElementsByTagName("*");
    }

    public Document getXmlDocument() {
        return xmlDocument;
    }
}
